<?php

namespace backend\controllers;

use Yii;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
/**
 * ArticleController implements the CRUD actions for Article model.
 */
class BaseController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }
    /**
     * [files description]
     * @return [type] [description]
     */
    public function files(){
        if(empty($_FILES))
            return false;
        return $_FILES;
    }
    /**
     * 通用上传
     * @return [type] [description]
     */
    public function fileupload($file)
    {   
        $file = array_keys($file);
        $files = UploadedFile::getInstancesByName(reset($file));
        $img = false;
        foreach ($files as $filenum => $fileval) {
            $dir = '/uploads/'.date('Y').'/'.date('m').'/'.date('d');
            $path = $dir.'/'.uniqid().'_'.$fileval->name;
            if(!is_dir($dir)){
                @mkdir(Yii::getAlias("@webroot").$dir,0777,true);
            }
            @move_uploaded_file($fileval->tempName,Yii::getAlias("@webroot").'/'.$path);
            $img = $path;
        }
        return $img;
    }
    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ]
        ];
    }

}
